﻿using Store.Model.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Store.ViewModel;

namespace Store.Dal
{
    public class TypeeDAL
    {
        private readonly StoreContext Db;
        public TypeeDAL(StoreContext _db)
        {
            Db = _db;
        }



        /// <summary>
        /// 查询  显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<TypeeModel> GetTypeeList(int page, int limit, out int total)
        {
            var query = from t in Db.Typees
                        where t.TypeState==1
                        select new TypeeModel
                        {
                            TypeId=t.TypeId,
                            TypeName=t.TypeName,
                            Tcid=t.Tcid,
                            Icon=t.Icon,
                            Ranks=t.Ranks,
                            Counts=t.Counts,
                            Tstate=t.Tstate,
                            Describe=t.Describe,
                            CreateTime=t.CreateTime,
                            CreatePeople=t.CreatePeople,
                            EalterPeople=t.EalterPeople,
                            EalterTime=t.EalterTime
                        };
            //获取总条数
            total = query.Count();
            //db.Configuration.ProxyCreationEnabled = false;
            //分页
            var list = query.Skip((page - 1) * limit)
                            .Take(limit)
                            .ToList();
            //传参
            return list;
        }


        /// <summary>
        /// 修改分类信息
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int UpdTypee(Typee t)
        {
            var data = Db.Typees.Find(t.TypeId);
            if (data != null)
            {
                data.TypeName = t.TypeName;
                data.Tcid = t.Tcid;
                data.Icon = t.Icon;
                data.Ranks = t.Ranks;
                data.Counts = t.Counts;
                data.Tstate = t.Tstate;
                data.Describe = t.Describe;
                data.CreateTime = t.CreateTime;
                data.CreatePeople = t.CreatePeople;
                data.EalterPeople = t.EalterPeople;
                data.EalterTime = t.EalterTime;

                return Db.SaveChanges();
            }
            return -1;
        }

        /// <summary>
        /// 修改分类状态
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int UpdTypeeState(Typee t)
        {
            var data = Db.Typees.Find(t.TypeId);
            if(data!=null)
            {
                data.Tstate = t.Tstate;

                return Db.SaveChanges();
            }
            return -1;
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="typee"></param>
        /// <returns></returns>
        public int TypeeAdd(Typee typee)
        {
            Db.Typees.Add(typee);
            return Db.SaveChanges();
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public int TypeeDel(int sid)
        {
            var sche = Db.Typees.Find(sid);
            if (sche != null)
            {
                //真删
                //Db.Typees.Remove(sche);

                //假删
                sche.TypeState = 0;

                return Db.SaveChanges();
            }
            return -1;
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int TypeeDelList(string[] ids)
        {
            var query = from s in Db.Typees
                        where ids.Contains(s.TypeId.ToString())
                        select s;
            if (query.Count() > 0)
            {
                //真删
                //Db.Typees.RemoveRange(query.ToList());

                //假删
                foreach (var item in query)
                {
                    item.TypeState = 0;
                }
                return Db.SaveChanges();
            }
            return -1;
        }
    }
}
